package org.zpmis.api.http;

import org.zpmis.api.business.UserWebService;
import org.zpmis.common.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.domain.user.qo.LoginQo;

import javax.annotation.Resource;

/**
* created with CodeGenerator
* description: 用户登录 和退出， 后台和小程序公用一个
* @author  Yang
* date: 2021-6-2
* time: 14:44:14
*/
@RestController
@RequestMapping("/login")
@Slf4j
public class UserController extends BaseController{

    @Resource
    private UserWebService userWebService;


    /**
     * 根据用户名密码  或者 微信code登录系统
     * @param loginQo
     * @return
     */
    @PostMapping("/userLogin")
    public PlatformResult login (@RequestBody LoginQo loginQo){
        try {
            return userWebService.login(loginQo);
        } catch (BusinessException e) {
            log.error("[UserController]---login--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[UserController]---login--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/userLogout")
    public PlatformResult logout (){
        try {
            userWebService.logout();
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[UserController]---logout--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[UserController]---logout--->Exception:",e);
            return PlatformResult.failure();
        }
    }
}
